package flc.ast.utils;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;

import flc.ast.R;
import stark.common.basic.utils.BroadcastReceiverUtil;

public class BatteryUtil {
    public static String technology;
    public static int voltage;
    public static int level;
    public static double temperature;
    public static String status;
    public static String health;
    public static String plugged;
    public static double batteryCapacity = 0;

    /**
     * 获取电池容量
     */
    public static void getBatteryCapacity(Context context) {
        Object mPowerProfile;

        final String POWER_PROFILE_CLASS = "com.android.internal.os.PowerProfile";
        try {
            mPowerProfile = Class.forName(POWER_PROFILE_CLASS)
                    .getConstructor(Context.class)
                    .newInstance(context);

            batteryCapacity = (double) Class
                    .forName(POWER_PROFILE_CLASS)
                    .getMethod("getBatteryCapacity")
                    .invoke(mPowerProfile);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 通过接收系统广播获取电池的信息
     */
    public static void ReceiverBatteryOtherInfo(Context context) {
        IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        Intent receiver = BroadcastReceiverUtil.registerReceiver(null, filter);

        technology = receiver.getStringExtra("technology"); //获取电池技术
        if (technology.equals("") || technology.equals(null)) {
            technology = "Unknown";
        }
        voltage = receiver.getIntExtra("voltage", 0); //获取电压(mv)
        level = receiver.getIntExtra("level", 0); //获取当前电量
        temperature = receiver.getIntExtra("temperature", 0) / 10.0; //获取温度(数值)并转为电池摄氏温度

        //电池状态
        switch (receiver.getIntExtra("status", 0)) {
            case BatteryManager.BATTERY_HEALTH_UNKNOWN:
                status = context.getString(R.string.battery_unknown);
                break;
            case BatteryManager.BATTERY_STATUS_CHARGING:
                status = context.getString(R.string.battery_charging);
                break;
            case BatteryManager.BATTERY_STATUS_DISCHARGING:
                status = context.getString(R.string.battery_discharging);
                break;
            case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
                status = context.getString(R.string.battery_uncharged);
                break;
            case BatteryManager.BATTERY_STATUS_FULL:
                status = context.getString(R.string.battery_full);
                break;
        }

        //电池健康情况
        switch (receiver.getIntExtra("health", 0)) {
            case BatteryManager.BATTERY_HEALTH_UNKNOWN:
                health = context.getString(R.string.battery_unknown);
                break;
            case BatteryManager.BATTERY_HEALTH_GOOD:
                health = context.getString(R.string.battery_fine);
                break;
            case BatteryManager.BATTERY_HEALTH_OVERHEAT:
                health = context.getString(R.string.battery_overheated);
                break;
            case BatteryManager.BATTERY_HEALTH_DEAD:
                health = context.getString(R.string.battery_flat);
                break;
            case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
                health = context.getString(R.string.battery_overvoltage);
                break;
            case BatteryManager.BATTERY_HEALTH_COLD:
                health = context.getString(R.string.battery_low);
                break;
            case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
                health = context.getString(R.string.battery_error);
                break;
        }

        //充电类型
        switch (receiver.getIntExtra("plugged", 0)) {
            case BatteryManager.BATTERY_PLUGGED_AC:
                plugged = context.getString(R.string.battery_charger);
                break;
            case BatteryManager.BATTERY_PLUGGED_USB:
                plugged = "USB";
                break;
            case BatteryManager.BATTERY_PLUGGED_WIRELESS:
                plugged = context.getString(R.string.battery_wireless);
                break;
            default:
                plugged = context.getString(R.string.battery_uncharged);
                break;
        }
    }
}